const { url, dbname, collections } = require("./mongo_conf");
const { MongoClient, ObjectId } = require("mongodb");

function createObjectId(id) {
    return ObjectId(id);
}

function handleErr(db, err, reject) {
    if (err) {
        db.close();
        reject(err);
        return true;
    }
    return false;
}

function insertOne(collectionName, data) {
    return new Promise((resolve, reject) => {
        MongoClient.connect(url, function (err, db) {
            handleErr(db, err, reject);
            var dbo = db.db(dbname);
            dbo.collection(collectionName).insertOne(data, function (err, res) {
                handleErr(db, err, reject);
                db.close();
                resolve({ msg: "文档插入成功", res });
            });
        });
    });
}

function deleteOne(collectionName, whereOption) {
    return new Promise((resolve, reject) => {
        MongoClient.connect(url, function (err, db) {
            handleErr(db, err, reject);
            var dbo = db.db(dbname);
            dbo.collection(collectionName).deleteOne(
                whereOption,
                function (err, res) {
                    handleErr(db, err, reject);
                    db.close();
                    resolve({ msg: "文档删除成功", res });
                }
            );
        });
    });
}

function updateOne(collectionName, whereOption, data) {
    return new Promise((resolve, reject) => {
        MongoClient.connect(url, function (err, db) {
            handleErr(db, err, reject);
            var dbo = db.db(dbname);
            dbo.collection(collectionName).updateOne(
                whereOption,
                { $set: data },
                function (err, res) {
                    handleErr(db, err, reject);
                    db.close();
                    resolve({ msg: "文档更新成功", res });
                }
            );
        });
    });
}

function find(collectionName, whereOption) {
    return new Promise((resolve, reject) => {
        MongoClient.connect(url, function (err, db) {
            handleErr(db, err, reject);
            var dbo = db.db(dbname);
            dbo.collection(collectionName)
                .find(whereOption)
                .toArray(function (err, data) {
                    handleErr(db, err, reject);
                    db.close();
                    resolve({ msg: "查询OK", data });
                });
        });
    });
}

function getAllData(collectionName) {
    return new Promise((resolve, reject) => {
        MongoClient.connect(url, function (err, db) {
            if (err) {
                db.close();
                reject(err);
                return;
            }

            var dbo = db.db(dbname);
            dbo.collection(collectionName)
                .find({})
                .toArray(function (err, result) {
                    db.close();

                    // 返回集合中所有数据
                    if (err) {
                        reject(err);
                    } else {
                        // console.log(result);
                        resolve(result);
                    }
                });
        });
    });
}

module.exports = {
    insertOne,
    deleteOne,
    updateOne,
    find,
    getAllData,
    createObjectId,
};
